package de.tum.wi.som.rcpsp.entities;

import java.util.ArrayList;
import java.util.List;

public class TimePeriod {
	private List<ResourceAssignment> resourceAssignments = new ArrayList<ResourceAssignment>();
	private int time;
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + time;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TimePeriod other = (TimePeriod) obj;
		if (time != other.time)
			return false;
		return true;
	}
	private Schedule schedule;
	
	public Schedule getSchedule() {
		return schedule;
	}

	public void setSchedule(Schedule schedule) {
		this.schedule = schedule;
	}

	public TimePeriod(int time) {
		super();
		this.time = time;
	}
	
	public int getAvailableResourcesPerTime(){		
		int availableResources= getSchedule().getWorkContentResource().getLimit();
		for (ResourceAssignment resourceAssignment : resourceAssignments) {					
			availableResources-=resourceAssignment.getResourceUnits();
		}
		return availableResources;
		
	}
	
	
	public void addResourceAssignment(ResourceAssignment assignment) {
		this.resourceAssignments.add(assignment);
	}
	public List<ResourceAssignment> getResourceAssignments() {
		return resourceAssignments;
	}
	public void setResourceAssignments(List<ResourceAssignment> resourceAssignment) {
		this.resourceAssignments = resourceAssignment;
	}
	public int getTime() {
		return time;
	}
	public void setTime(int time) {
		this.time = time;
	}

	public void removeResourceAssignment(ResourceAssignment resAssignment) {
		resourceAssignments.remove(resAssignment);
	}
	
	@Override
	public String toString() {
		return new Integer(getTime()).toString();
	}
	
	public String getAllAcitivitiesAsString(){
		String result="";
		for (ResourceAssignment resAssign : resourceAssignments) {
			result += ", " + resAssign.getActivity() + "(" + resAssign.getResourceUnits() + ")";
		}
		return result;
	}

	public int getResourceValueForActivity(Activity activity) {
		for (ResourceAssignment resAssignment : resourceAssignments) {
			if(resAssignment.getActivity().equals(activity)){
				return resAssignment.getResourceUnits();
			}
		}
		return 0;
	}
}
